import Vue from 'vue'
import App from './App'
import Router from 'vue-router'

import store from './store'
import home from './components/home'
import commit from './components/commit'


Vue.config.productionTip = false

Vue.use(Router)

const routes = [
  {path: '/',name: 'home',component: home,meta: { requiresAuth: true }},
  {path: '/commit', name: 'commit',component: commit,meta: {requiresAuth: true },
      children:[
          {
              path: ':id'
          }
      ]
    }
]


const router = new Router({
  routes
})

function getURLParameter (name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null
}
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('authToken')
    const url = location.href
    if (token) {
      next()
    } else {
      // 重定向获取Token
      location.href = '/user_base/user/get_code' + '?url=' + url
      const token = getURLParameter('token')
      if (token) {
        localStorage.setItem('authToken', token)
      }
    }
  }
  next()
})

new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})